<?php
/** 
 *
 * @package ucp
 * @version $Id: ucp_phpbb_openid.php 77 2010-02-28 07:37:17Z jonzenor $
 * @copyright (c) 2009 ZenorSoft Web Design (http://www.ZenorSoft.com)
 * @license http://opensource.org/licenses/gpl-license.php GNU General Public License 
 */

/**
 * @ignore
 */
if (!defined('IN_PHPBB'))
{
    exit;
}

/**
* @package ucp
*/
class ucp_phpbb_openid
{
    var $u_action;
    var $tpl_path;
    var $page_title;

    function main($id, $mode)
    {
		global $db, $phpbb_root_path, $phpEx, $user, $template;
		
		include($phpbb_root_path . "includes/openid/openid_class." . $phpEx);
		include($phpbb_root_path . "includes/openid/openid_config." . $phpEx);
		
		$phpbb_openid = new phpbb_openid;
		
		// Setup mod required items
			// Comment out until we have a lang file
		$user->lang['TITLE'] = "phpBB OpenID";
		$this->tpl_name	= 'ucp_phpbb_openid';
		$this->page_title	= 'UCP_OPENID_TITLE';
		$submit = isset($_POST['submit']) ? true : false;
		$authopenid = isset($_GET['authopenid']) ? true : false;
		$action = isset($_GET['action']) ? request_var('action', '') : false;
		$delete = $action == "DelID" ? true : false;

		// Get the user's id
		$user_id = $user->data['user_id'];
		
		// Show Version info
		$template->assign_vars(array(
			'S_OPENID_VERSION'	=> $oid_config['version'],
			'S_OPENID_DATE'	=> $oid_config['date'],
			'S_DOWNLOAD_LINK'	=> 'http://zenorsoft.com/phpBB3/viewtopic.php?f=30&t=841',
			'S_OPENID_CONTACT'	=> '<a href="http://zenorsoft.com/phpBB3">MasterZ on ZenorSoft Web Design Forums</a>',
		));
		
		
		// Add URL
		if ($submit)
		{
			// get id
			$new_openid = request_var('openid_url', '');
			
			// Start OpenID
			require($phpbb_root_path . 'includes/openid/common.' . $phpEx);
			$auth_request = $consumer->begin($new_openid);
			
			// validate OpenID
			if (!$auth_request)
			{
				trigger_error($user->lang['OPENID_ERROR_INVALID']);
			}
			
			$ucp_i = request_var('i', '');
			
			// Save openid temp
			
			$phpbb_openid->setup_url('ucp');	
			$phpbb_openid->processOID($auth_request);
			$trust_root = $phpbb_openid->getTrustRoot();
			$process_url = $phpbb_openid->getReturnURL();
//			$redirect_url = $auth_request->redirectURL($trust_root, $process_url);
//			header("Location: " . $redirect_url);
		}

		if ($authopenid)
		{
			// Start OpenID
			require($phpbb_root_path . 'includes/openid/common.' . $phpEx);
			
			// Get openid status
			$openid_return_to = request_var('openid_return_to', '');
			$response = $consumer->complete($openid_return_to);
			
			$passchecks = true;
			
			// See if auth passed
			if ($passchecks)
			{
				if ($response->status != Auth_OpenID_SUCCESS)
				{
					add_oid_message($user->lang['OPENID_ERROR_AUTH_FAIL'] . $response->message);
					$passchecks = false;
				}
			}
			
			// See if openid exists already
			if ($passchecks)
			{
				$new_openid = $response->identity_url;
				
				$openid_exists = $phpbb_openid->findOpenID($new_openid);
				
				if ($openid_exists)
				{
					// Already exists
					add_oid_message($user->lang['OPENID_ERROR_ID_EXISTS']);
					$passchecks = false;
				}
			}
			
			// Save
			if ($passchecks)
			{
				$user_id = $user->data['user_id'];
				
				$phpbb_openid->bind($user_id, $new_openid);
				
				add_oid_message($user->lang['OPENID_ADDED']);
			}
		}
		
		if ($delete)
		{
			// See if confirmation set
			$confirm = isset($_GET['confirm']) ? true : false;
			$oid_id = request_var('oid', 0);

			// Make sure the id belongs to this user & get url
			$check_url = $phpbb_openid->findByID($oid_id, $user_id);

			if (!$check_url)
			{
				trigger_error($user->lang['OPENID_ERROR_DEL_REQ']);
			}
			
			// Was deletion confirmed?
			// If not, confirm
			if (!$confirm)
			{ // XXXXXX ADD LANG VAR HERE XXXXXX //
				add_oid_message($user->lang['OPENID_DEL_CONF'] . $check_url . " <a href='ucp.php?i=" . request_var('i', '') . "&action=DelID&oid=" . $oid_id . "&confirm=yes'>" . $user->lang['OPENID_DELETE'] . "</a> | <a href='ucp.php?i=" . request_var('i', '') . "'>" . $user->lang['CANCEL'] . "</a>");
			}
			else
			{
				// Delete the id
				$deleted = $phpbb_openid->deleteOpenID($user_id, $oid_id);

				if ($deleted)
				{
					add_oid_message($user->lang['OPENID_DELETED']);
				}
				else
				{
					add_oid_message($user->lang['OPENID_ERROR_DELETING']);
				}
			}
		}
		
		// List URLS
		if ($mode == "view")
		{
			
			// Get URL's
			$sql = 'SELECT * FROM ' . OPENID_URLS_TABLE . ' WHERE 
					user_id = "' . $user_id . '"';

			$result = $db->sql_query($sql);
			$delete_link = "ucp.php?i=" . request_var('i', '') . "&action=DelID";
			
			while ($row = $db->sql_fetchrow($result))
			{
				$template->assign_block_vars('openid', array(
					'OPEN_ID'	=> $row['openid_url'],
					'ID'		=> $row['id'],
					'DELETE_LINK'	=> $delete_link . "&oid=" . $row['id'],
				));
			}
			$db->sql_freeresult($result);
		}
		
		$template->assign_vars(array(
			'S_MODE'	=> $mode,
		));
    }
}

function add_oid_message($text)
{
	global $template;
	$template->assign_block_vars('openid_messages', array('MESSAGE'	=> $text));
//	echo $text . "<br />";
}
?>